﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Asomy.Model;
using System.Windows.Controls;
using System.Windows;

namespace Asomy.View
{
    class LayerViewItem
    {
        public StackPanel m_content = new StackPanel();
        TextBox m_layername;
        StackPanel m_checkboxesPanel;

        CheckBox m_visibility = new CheckBox();
        CheckBox m_locked = new CheckBox();

        public int m_layerID;

        public LayerViewItem(Layer a_layer)
        {
            m_layerID = a_layer.m_id;

            //sets content to stack each element vertically
            m_content.Orientation = Orientation.Vertical;

            //text box that holds the name (you can write in this one)
            m_layername = new TextBox();

            m_layername.Text = a_layer.m_name;
            m_layername.MaxLength = 15;
            //if you click away from the textbox it will try to rename it
            m_layername.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(SetLayerName));
            
            //creates a new stackpanel to store all the checkboxes horizontally
            m_checkboxesPanel = new StackPanel();

            m_checkboxesPanel.Orientation = Orientation.Horizontal;           


            if (a_layer.Visible)
            {
                m_visibility.IsChecked = true;
            }
            else
            {
                m_visibility.IsChecked = false;
            }

            m_visibility.Content = "Vis ";
            m_visibility.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(ShowLayer));
            m_visibility.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(HideLayer));

            if (a_layer.Locked)
            {
                m_locked.IsChecked = true;
            }
            else
            {
                m_locked.IsChecked = false;
            }

            m_locked.Content = "Lock ";
            m_locked.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(LockLayer));
            m_locked.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(UnLockLayer));

            //checkboxpanels adds
            m_checkboxesPanel.Children.Add(m_visibility);  
            m_checkboxesPanel.Children.Add(m_locked);

            Button f_delete = new Button();
            f_delete.Height = 25;
            f_delete.Content = "Delete Layer";

            f_delete.AddHandler(Button.ClickEvent, new RoutedEventHandler(DeleteLayer));

            //contents adds
            m_content.Children.Add(m_layername);
            m_content.Children.Add(m_checkboxesPanel);
            m_content.Children.Add(f_delete);
        }

        private void SetLayerName(object sender, RoutedEventArgs e)
        {
            if (!LayerHandler.RenameLayer(m_layerID, m_layername.Text))
            {
                if (!LayerHandler.m_needUpdate)
                {
                    m_layername.Text = LayerHandler.m_layers[m_layerID].m_name;
                }
            }
        }

        private void ShowLayer(object sender, RoutedEventArgs e)
        {

            LayerHandler.ShowLayer(m_layerID);
        }

        private void HideLayer(object sender, RoutedEventArgs e)
        {
            LayerHandler.HideLayer(m_layerID);
        }

        private void DeleteLayer(object sender, RoutedEventArgs e)
        {
            LayerHandler.DeleteLayer(m_layerID);
        }

        private void LockLayer(object sender, RoutedEventArgs e)
        {            
            LayerHandler.LockLayer(m_layerID);            
        }

        private void UnLockLayer(object sender, RoutedEventArgs e)
        {
            LayerHandler.UnLockLayer(m_layerID);            
        }        
    }
}
